//
//  VVEmoticonViewCell.swift
//  Weibo
//
//  Created by 王惠平 on 2017/4/7.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit

class VVEmoticonViewCell: UICollectionViewCell {
    
    fileprivate lazy var messageLabel: UILabel = {
        
       let label = UILabel(fonSize: 20, textColor: UIColor.white, text: "")
        
       return label
        
    }()
    
    //删除按钮
    fileprivate lazy var deleteButton: UIButton = {
       
        let button = UIButton()
        
        //点击事件
        button.addTarget(self, action: #selector(dileteButtonAction), for: .touchUpInside)
        
        button.setImage(UIImage(named:"compose_emotion_delete"), for: .normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), for: .highlighted)
        
        return button
    }()
    
    //表情按钮数组
    fileprivate lazy var emoticonButtonArray: [VVEmoticonButton] = [VVEmoticonButton]()
    
    //表情数据
    var emoticonArray: [VVEmoticon]? {
        
        didSet {
            
            guard let currentEmoticonArray = emoticonArray else {
                
                return
            }
            
            //默认表情全部隐藏
            for button in emoticonButtonArray {
                
                button.isHidden = true
                
            }
            
            //遍历表情数组模型给表情按钮赋值
            for (i, emoticon) in currentEmoticonArray.enumerated() {
                
                //根据下标获取表情按钮
                let button = emoticonButtonArray[i]
                
                button.emoticon = emoticon
                
//                button.isHidden = false
//                
//                if emoticon.type == "0" {
//                    
//                    //图片表情
//                    button.setImage(UIImage(named:emoticon.path!), for: .normal)
//                    button.setTitle(nil, for: .normal)
//                    
//                } else {
//                    
//                    //emoji表情
//                    let emoji = (emoticon.code! as NSString).emoji()
//
//                    button.setTitle(emoji, for: .normal)
//                    button.setImage(nil, for: .normal)
//                    
//                }
            }
            
        }
        
    }
    
    var indexPath: IndexPath? {
        
        didSet {
            
            guard let currentIndexPath = indexPath else {
                
                return
            }
            
            messageLabel.text = "当前显示的是第\(currentIndexPath.section + 1)组第\(currentIndexPath.item + 1)页"
            
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加子控件设置约束
    fileprivate func setupUI() {
        
        //添加20个表情按钮
        addChildButton()
        contentView.addSubview(deleteButton)
        
//        contentView.addSubview(messageLabel)
//        
//        messageLabel.snp.makeConstraints { (make) in
//            
//            make.center.equalTo(contentView)
//        }
        
    }
    
    //添加20个表情按钮
    fileprivate func addChildButton() {
        
        //提示:如果再此获取不到当前视图的宽度,需要在layoutsubView里面去子控件布局
        for _ in 0..<20 {
            
            let button = VVEmoticonButton()
            
            //点击事件
            button.addTarget(self, action: #selector(emoticonButtonAction(btn:)), for: .touchUpInside)
            
            //设置文字大小
            button.titleLabel?.font = UIFont.systemFont(ofSize: 33)
            
            //button.backgroundColor = RandomColor()
            
            contentView.addSubview(button)
            
            //保存表情按钮
            emoticonButtonArray.append(button)
        }
    }
    
   @objc private func emoticonButtonAction(btn: VVEmoticonButton) {
        
        let emoticon = btn.emoticon
        
        //发送通知给textView设置值
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedEmoitconButtonNoti), object: emoticon)
    
    }
    
   @objc private func dileteButtonAction() {
        
        //发送删除表情的通知
        NotificationCenter.default.post(name: NSNotification.Name(DidSelectedDeleteEmoticonNoti), object: nil)
        
    }
    
    //完成子控件的布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //每项的宽度
        let buttonWidth = width / 7
        
        //每项的高度
        let buttonHeight = height / 3
        
        for (i,button) in emoticonButtonArray.enumerated() {
            
            //获取列索引
            let  colIndex = i % 7
            
            //获取行索引
            let  rowIndex = i / 7
            
            //设置x坐标
            button.x = CGFloat(colIndex) * buttonWidth
            
            //设置y坐标
            button.y = CGFloat(rowIndex) * buttonHeight
            
            //设置大小
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
            
        }
        
        //设置删除按钮大小
        deleteButton.size = CGSize(width: buttonWidth, height: buttonHeight)
        
        deleteButton.x = width - buttonWidth
        
        deleteButton.y = height - buttonHeight
    }
    
}
